package test;

import java.util.InputMismatchException;
import java.util.Scanner;

public class TryDemo2 {
    public static void main(String[] args) {
        /*
        try {
            int result = test();
            System.out.println("one和two的商是:" + result);
        } catch (ArithmeticException e) {
            System.out.println("除数不允许为零");
            e.printStackTrace();
        } catch (InputMismatchException e) {
            System.out.println("请输入整数");
            e.printStackTrace();
        } finally {
            System.out.println("======运算结束======");
        }
         */
        try {
            int result = test();
            System.out.println("one和two的商是:" + result);
        }catch (ArithmeticException e) {
            System.out.println("除数不允许为零");
            e.printStackTrace();
        }catch (InputMismatchException e) {
            e.printStackTrace();
        }catch (Exception e) {
            System.out.println("请输入整数");
            e.printStackTrace();
        } finally {
            System.out.println("======运算结束======");
        }
    }

    /*
    通过throws抛出异常时,针对可能出现的多种异常情况,解决方案:
    1.throws后面接多个异常类型,中间用逗号分隔
    2.在throws后面接Exception
     */
    /*
    //1.
    public static int test() throws ArithmeticException,InputMismatchException{
        //要求定义两个整数,接收用户输入,输出两数之商
        Scanner input = new Scanner(System.in);
        System.out.println("======运算开始======");
        System.out.print("请输入第一个整数:");
        int one = input.nextInt();
        System.out.print("请输入第二个整数:");
        int two = input.nextInt();
        return one / two;
    }
     */
    public static int test() throws Exception{
        //要求定义两个整数,接收用户输入,输出两数之商
        Scanner input = new Scanner(System.in);
        System.out.println("======运算开始======");
        System.out.print("请输入第一个整数:");
        int one = input.nextInt();
        System.out.print("请输入第二个整数:");
        int two = input.nextInt();
        return one / two;
    }
}
